<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="收货人" prop="receivePerson">
        <el-input
          v-model="queryParams.receivePerson"
          placeholder="请输入收货人姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="下单电话" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入下单电话"
          clearable
        />
      </el-form-item>
      <el-form-item label="配送地址" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择配送地址">
          <el-option
            v-for="item in shopList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="配送状态" prop="status">
        <el-select v-model="queryParams.deliveryStatus" placeholder="请选择">
          <el-option
            v-for="item in deliveryStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList"  @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.orderDetails">
            <el-table-column label="主图" align="center" prop="primaryImage" width="100">
              <template slot-scope="scope">
                <el-image
                  lazy

                  fit="contain"
                  :preview-src-list="scope.row.primaryImage|getAvatarList"
                  :src="scope.row.primaryImage|getAvatar">
                </el-image>
              </template>
            </el-table-column>
            <el-table-column label="商品名称" prop="title" align="center" />
            <el-table-column label="购买数量" prop="supNum" align="center">
              <template slot-scope="scope">
                <el-tag type="success">{{'x'+scope.row.supNum}}</el-tag>
              </template>

            </el-table-column>
            <el-table-column label="所需积分" prop="supScore" align="center">
              <template slot-scope="scope">
                <span>{{'积分'+(scope.row.supNum*scope.row.supScore)}}</span>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="收货人" align="center" prop="receivePerson" />
      <el-table-column label="下单电话" align="center" prop="userName" />
      <el-table-column label="配送地址" align="center" prop="shopId">
        <template slot-scope="scope">
          <el-tag type="success">{{scope.row.shopId|shopFilter(shopListMap)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="配送状态" align="center" prop="deliveryStatus">
        <template slot-scope="scope">
          <el-tag :type="scope.row.deliveryStatus==='3'?'success':'danger'">{{scope.row.deliveryStatus|deliveryFilter(deliveryStatusMap)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单积分" align="center" prop="totalAmount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="配送状态" prop="deliveryStatus">
          <el-select v-model="form.deliveryStatus" placeholder="请选择配送状态">
            <el-option
              v-for="item in deliveryStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配送地址" prop="shopId">
          <el-select v-model="form.shopId" placeholder="请选择配送地址">
            <el-option
              v-for="item in shopList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货人" prop="receivePerson">
          <el-input v-model="form.receivePerson" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/system/order"
import {getDicts} from "@/api/system/dict/data";
import {DICT_TYPE, getAvatar, getAvatarList} from "../../../utils/jpUtils";
import {listShop} from "../../../api/system/shop";

export default {
  name: "Order",
  data() {
    return {
      deliveryStatusMap:new Map(),
      shopListMap:new Map(),
      deliveryStatus:[],
      shopList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        receivePerson:null,
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        userName: null,
        shopId: null,
        deliveryStatus: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deliveryStatus: [
          { required: true, message: "配送状态不能为空", trigger: "blur" }
        ],
        shopId: [
          { required: true, message: "配送地址不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getShopList()
    this.initDic()
  },
  filters:{
    getAvatarList,
    getAvatar,
    shopFilter(val,shopListMap){
      return shopListMap.get(val)
    },
    deliveryFilter(val,deliveryMap){
      return deliveryMap.get(val)
    }
  },
  methods: {
    /** 查询商品订单列表 */
    getList() {
      this.loading = true
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    initDic(){
      getDicts(DICT_TYPE.DELIVERY_STATUS).then(res=>{
       let data = res.data
        let deliveryStatus=[]
        if(data){
          let map = new Map();
          data.forEach(item=>{
            map.set(item.dictValue,item.dictLabel);
            deliveryStatus.push({ value: item.dictValue,label:item.dictLabel})
          })
          console.log(deliveryStatus)
          this.deliveryStatus = deliveryStatus;
          this.deliveryStatusMap = map;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNo: null,
        userId: null,
        shopId: null,
        deliveryStatus: null,
        totalAmount: null,
        remark: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询配送店列表 */
    getShopList() {
      listShop({}).then(res => {
        let data = res.rows
        let shopList =[]
        let  map = new Map()
        if(data){

          data.forEach(item=>{
            map.set(item.id,item.name);
            shopList.push({value:item.id,label:item.name})
          })
        }
        this.shopList = shopList
        this.shopListMap = map
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改商品订单"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids
      this.$modal.confirm('是否确认删除商品订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
